Knowledge Base

Working With Custom Hooks: OnBeforePublish

Publishing in the CMS is the final step to sending content out to an external environment, usually to an Ingeniux DSS. Learn how this custom hook can be employed to avoid publishing mistakes.


Publishing in the CMS is the final step to sending content out to an external environment, usually to an Ingeniux DSS.  

The publish can be run as incremental or full. Incremental publishes add new content that has been checked in and marked for publish to the specified publishing target since the last publish was run, while a full publish republishes every content item that is checked in and marked for publish to the specified publishing target. 

Administrators can control who is allowed to publish using permissions for incremental and full publishing, and who can publish to a specific target using security. This hook is for controlling anything outside of those controls. 

How To Trigger the Hook  

This method is triggered through the following actions: 

  • Clicking Publish from the toolbar on a content item and completing the dialog. 
  • Right-clicking on a content item in the Site Tree or Asset Tree, selecting Publish and completing the dialog. 
  • Selecting a publishing target from Administration > Publishing System, pushing the New Publish button, and completing the dialog. 
  • Calling the publish() method from the CSAPI. 

When to Use This Custom Hook  

There are many reasons why you may want to take advantage of this hook. Sending content to a live site can be a complicated process in even simple websites, not to mention larger and more complex builds with multiple systems linking together. 

You may want to use this hook to update external content repositories to let them know there’s new content, notify a QA team that there is new content to be reviewed live, review the publishing content and make sure it is accurate, or to simply prevent publishing during heavy traffic hours. 

Considerations 

This method is slightly more precarious to use than other methods. Publishing is a vital part of the process of getting content into your live site. You definitely do not want to do too much to slow down your process here. Make sure your scripts are fast and efficient. 

If you plan to prevent publishing for any reason using this hook you will want to make sure you are educating your users as to why, communicating effectively with them during the cancellation, and logging the reasons and times these things happen. 

Example  

Prevent publish if the home page is unmarked for publish  

The home page of a site is set in the publishing target and all URLs and site structures build off that one page. There have been many occasions where an unintentional click has caused the home page of a site to become unmarked for publish, which would cause major issues if the site were subsequently published.  

This script prevents the publish from happening if that home page content item in the Site Tree is unmarked. 

First, gather the home page that is set for the publishing target being published. 

Then if that item is unmarked for publish, throw an exception. 

string homePageID = pubTarget.SURLSettings.HomePage; 

if (!pubTarget.Marked(homePageID)) 

{ 

      throw new Exception("HomePage is not marked for publish. Please go back and make sure the HomePage is marked for publish for" + pubTarget.Name); 

} 

Ways to Extend the Functionality Further  

More communication would be great for this hook. You are throwing an error but consider putting details in the logs and maybe sending an email. 

There are usually a few other items that are essential to a fully working website, so you could add a list of all those items to check. 

You could also consider moving the checks for those important items to hooks like OnBeforeMarkForPublish (when an item gets unmarked for publish) or OnBeforeDelete (deleting also unmarks for publish) so you catch the issue long before the publish is attempted. 

  • PRODUCT: CMS
  • VERSION: CMS 10
  • RELEASE: 10.x
  • Published: September 13, 2023
  • LAST UPDATED: September 18, 2023
  • Comments: 0

Please login to comment

Comments


There are no comments yet.